import axios from 'axios'
import {getDb} from './common'
import router from '../router'
let axiosInstance = axios.create({
  // baseURL: '/api',
  headers: {
    'Content-Type': 'application/json',
    // 'Accept': 'application/x.yaque.v2+json',
    'Access-Control-Allow-Origin': '*', 
    'Cache-Control': 'no-cach',
    'source': 4
  },
  withCredentials: 'include',
  transformRequest: [function (data) {
    if (data && data.constructor && data.constructor.name === 'FormData') {
      return data
    }
    return JSON.stringify(data)
  }]
})
axiosInstance.defaults.timeout = 600000
axiosInstance.interceptors.request.use(config => {
    const token = getDb('token') || ''
    if (!token) {
      router.push('/login')
    }
    config.headers['Authorization'] = `Bearer ${token}`
    return config
}, err => {
  return Promise.reject(err)
})
axiosInstance.interceptors.response.use(data => {
    if (data.status === 200) {
        return data
    }
}, error => {
    return Promise.reject(error)
})
export default axiosInstance
